//
//  JKDateTool.m
//  ZhiHuDaily
//
//  Created by albert on 16/8/19.
//  Copyright © 2016年 albert. All rights reserved.
//

#import "JKDateTool.h"

@implementation JKDateTool
/** 获取指定日期的前一天 参数格式：20160819 */
+ (int)getPreviousDayOfSpecifiedDate:(int)date{
    return [self getRelatedDateOfSpecifiedDate:date withYear:0 month:0 day:-1];
}

/** 获取指定日期的后一天 参数格式：20160819 */
+ (int)getNextDayOfSpecifiedDate:(int)date{
    return [self getRelatedDateOfSpecifiedDate:date withYear:0 month:0 day:1];
}

/** 获取今天的日期 返回值格式：20160819 */
+ (int)getDateOfToday{
    NSDate *today = [NSDate date];
    
    // 日期格式
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"yyyyMMdd";
    NSString *todayStr = [fmt stringFromDate:today];
    
    return [todayStr intValue];
}

/** 获取今天的日期和时间字符串 返回值格式：2016-08-19 12:59:00 */
+ (NSString *)getDateAndTimeStrOfToday{
    
    NSDate *today = [NSDate date];
    
    // 日期格式
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    
    return [fmt stringFromDate:today];
}

/** 判断两个日期是否在同一个星期 */
+ (BOOL)isDate:(int)date inTheSameWeekWithAnotherDate:(int)anotherDate{
    
    if (date == 0 || anotherDate == 0) return NO;
    
    if (date == anotherDate) return YES;
    
    date = [self getMondayOfSpecifiedDate:date];
    anotherDate = [self getMondayOfSpecifiedDate:anotherDate];
    
    return date == anotherDate;
}

/** 获取指定日期的周一的日期 */
+ (int)getMondayOfSpecifiedDate:(int)date{
    NSString *monday = [self getWeekdayOfSpecifiedDate:date];
    if ([monday isEqualToString:@"星期一"]) return date;
    
    for (int i = 0; i < 7; i++) {
        
        date = [self getPreviousDayOfSpecifiedDate:date];
        monday = [self getWeekdayOfSpecifiedDate:date];
        
        if ([monday isEqualToString:@"星期一"]) {
            break;
        }
    }
    
    return date;
}

/** 获取今天的星期 */
+ (NSString *)getWeekdayOfToday{
    return [self getWeekdayOfSpecifiedDate:[self getDateOfToday]];
}

/** 获取指定日期的星期 参数格式：20160819 */
+ (NSString *)getWeekdayOfSpecifiedDate:(int)date{
    
    NSString *dateStr = @(date).stringValue;
    
    NSArray *weekdays = @[[NSNull null], @"星期日", @"星期一", @"星期二", @"星期三", @"星期四", @"星期五", @"星期六"];
    
    // 日期格式
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"yyyyMMdd";
    NSDate *specifiedDate = [fmt dateFromString:dateStr];
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    //NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    
    //[calendar setTimeZone: timeZone];
    
    NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;
    
    NSDateComponents *comp = [calendar components:calendarUnit fromDate:specifiedDate];
    
    return [weekdays objectAtIndex:comp.weekday];
}

/** date参数格式：2016081。; year=1表示1年后的时间 year=-1为1年前的日期 month day 类推 */
+ (int)getRelatedDateOfSpecifiedDate:(int)date withYear:(int)year month:(int)month day:(int)day {
    NSString *dateStr = @(date).stringValue;
    
    // 日期格式
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"yyyyMMdd";
    NSDate *specifiedDate = [fmt dateFromString:dateStr];
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    // 比较日期
    NSDateComponents *deltaComp = [[NSDateComponents alloc] init];
    
    // 这个是获取当前时间差的date的 其中 year=1表示1年后的时间 year=-1为1年前的日期 month day 类推
    [deltaComp setYear:year];
    
    [deltaComp setMonth:month];
    
    [deltaComp setDay:day];
    
    NSDate *newDate = [calendar dateByAddingComponents:deltaComp toDate:specifiedDate options:0];
    
    NSString *newDateStr = [fmt stringFromDate:newDate];
    
    return [newDateStr intValue];
}
@end
